package com.taptech.doufu.ui.viewholder.personalcenter;

import android.content.Context;
import android.graphics.drawable.Animatable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.controller.BaseControllerListener;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.image.ImageInfo;
import com.taptech.doufu.util.TextUtil;
import com.taptech.doufu.R;
import com.taptech.doufu.app.WeMediaApplication;
import com.taptech.doufu.bean.AlbumsBean;
import com.taptech.doufu.bean.MineAbstractBean;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.ui.view.drawcircle.BaseRecyclerViewViewHolder;
import com.taptech.doufu.util.ImageManager;
import com.taptech.doufu.util.ScreenUtil;
import com.taptech.doufu.util.StartActivityUtil;

/**
 * Created by lsy on 2015/10/8.
 */
public class AlbumsRecyclerViewHolder extends BaseRecyclerViewViewHolder implements View.OnClickListener {

    Context mContext ;
    SimpleDraweeView imageView;
    TextView titleTextView;
    View albumsLayout;



    AlbumsBean bean;

    public AlbumsRecyclerViewHolder(Context context, View itemView) {
        super(context, itemView);
        mContext = context;
        albumsLayout =itemView.findViewById(R.id.albums_item_layout);
        imageView = (SimpleDraweeView) itemView.findViewById(R.id.album_imageview);
        titleTextView = (TextView) itemView.findViewById(R.id.album_title_textview);
        rankImageView = (ImageView) itemView.findViewById(R.id.home_rank_position);
        mCircleSource = new TextView(mContext);

    }

    @Override
    public void setViewHolderChildViewContent(MineAbstractBean tempbean, int position) {
        bean = (AlbumsBean) tempbean;
        if (bean == null){
            return;
        }
        changeModel();
        if (bean.getName() != null){
            titleTextView.setVisibility(View.VISIBLE);
            titleTextView.setText(bean.getName());
        }else {
            titleTextView.setVisibility(View.GONE);
        }
        if (bean.getIcon() != null && !TextUtil.isEmpty(bean.getIcon())){
            imageView.setVisibility(View.VISIBLE);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT , (int) (ScreenUtil.getScreenWidth(mContext)/3.0f));
            imageView.setLayoutParams(params);
            ImageManager.loadImage(imageView, bean.getIcon(), 3.0f  , new BaseControllerListener<ImageInfo>(){
                @Override
                public void onFinalImageSet(String id, ImageInfo imageInfo, Animatable animatable) {
                    super.onFinalImageSet(id, imageInfo, animatable);
                    if (titleTextView != null){
                        titleTextView.setVisibility(View.GONE);
                    }

                }
            });
            imageView.setOnClickListener(this);
        }else {
            imageView.setVisibility(View.GONE);
        }
        if (bean.isRank_flag()){
            setRankImageView(position + 1);
        }else if (rankImageView != null){
            rankImageView.setVisibility(View.GONE);
        }

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.album_imageview:
                StartActivityUtil.albumsOnclick(mContext , bean);
                break;
        }
    }

    private void changeModel(){
        if(WeMediaApplication.getInstance().isDayNightMode_Night){
            changeDarkModel();
        }else {
            changeDayModel();
        }
    }

    private void changeDayModel(){
        imageView.setBackgroundColor(WeMediaApplication.getInstance().getResources().getColor(R.color.transparent_background));
        imageView.setAlpha(1.0f);
        titleTextView.setTextColor(WeMediaApplication.getInstance().getResources().getColor(R.color.text_color_8));
        rankImageView.setAlpha(1.0f);
        albumsLayout.setBackgroundColor(WeMediaApplication.getInstance().getResources().getColor(R.color.white));
    }

    private void changeDarkModel(){
        imageView.setBackgroundColor(WeMediaApplication.getInstance().getResources().getColor(R.color.black));
        imageView.setAlpha(0.5f);
        rankImageView.setAlpha(Constant.imagViewAlpha);
        titleTextView.setTextColor(WeMediaApplication.getInstance().getResources().getColor(R.color.title_text_dark));
        albumsLayout.setBackgroundColor(WeMediaApplication.getInstance().getResources().getColor(R.color.bg_dark));
    }
}
